package com.example.mychatroom.controller;

import com.example.mychatroom.a_utils.SessionKey;
import com.example.mychatroom.model.Friend;
import com.example.mychatroom.model.MessageSession;
import com.example.mychatroom.model.User;
import com.example.mychatroom.serviceApi.FriendService;
import com.example.mychatroom.serviceApi.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/client")
public class ClientController {

    @Autowired
    public UserService userService;

    @Autowired
    public FriendService friendService;


    @RequestMapping("/userInfo")
    public Object getUserInfo(HttpServletRequest request){
        //1、先从请求中获取对话
        HttpSession session = request.getSession();
        //2、从会话中获取之前保存的用户对象
        if (session == null){
            System.out.println("getUserInfo 当前获取不到session对象");
            return new User();
        }else {
            User user = (User) session.getAttribute(SessionKey.USERINFO_SESSION_KEY);
            if (user == null){
                System.out.println("getUserInfo 当前获取不到user对象");
                return new User();
            }
            user.setPassword(null);
            return user;
        }
    }

    @RequestMapping("/friendList")
    public Object getFriendList(HttpServletRequest request){
        //1、先从会话当中获取Session对象
        HttpSession session = request.getSession(false);
        //2、从会话中获取之前保存的用户对象
        if (session == null){
            System.out.println("getFriendList 当前获取不到session对象");
            return new ArrayList<Friend>();
        }else {
            User user = (User) session.getAttribute(SessionKey.USERINFO_SESSION_KEY);
            if (user == null){
                System.out.println("getFriendList 当前获取不到user对象");
                return new ArrayList<Friend>();
            }
            //3、根据userAccount从数据库查询数据
            List<Friend> friendList = friendService.getFriendList(user.getUserAccount());
            return friendList;
        }
    }

    @RequestMapping("/sessionList")
    public Object getSessionList(HttpServletRequest request){
        //1、先从会话当中获取Session对象
        HttpSession session = request.getSession(false);
        //2、从会话中获取之前保存的用户对象
        if (session == null){
            System.out.println("getSessionList 当前获取不到session对象");
            return new ArrayList<Friend>();
        }else{
            User user = (User) session.getAttribute(SessionKey.USERINFO_SESSION_KEY);
            if (user == null){
                System.out.println("getSessionList 当前获取不到user对象");
                return new ArrayList<Friend>();
            }
            List<MessageSession> sessionList = friendService.getSessionList(user.getUserAccount());
            return sessionList;
        }
    }

    @RequestMapping("/session")
    public Object createSession(@SessionAttribute(SessionKey.USERINFO_SESSION_KEY) User user, @RequestParam("toUserAccount") String toUserAccount){
            return friendService.createSession(user.getUserAccount(),toUserAccount);
    }
}
